const { getAll, add, remove, getOne, getCount } = require('../utils/dbTools')
// 关注数据
const tableName = 'follow'

/**
 * 判断记录是否存在 
 * @param {number} uid 当前登录用户uid
 * @param {number} follow_uid 被关注用户id   
 * @return {boolean} true已关注;false未关注 
 */

const checkFollow = async (uid, follow_uid) => {
    const result = await getOne(tableName, { uid, follow_uid })
    if (result) {
        return true;
    }
    return false;
}

/**
 * 获取关注列表（自己关注的用户列表） 
 * @param {number} follow_uid 当前登录用户follow_uid
 * @return {array} 被关注用户列表
 */
const getFollowingList = async (follow_uid) => {
    const result = await getAll(tableName, { follow_uid });
    return result
}
/**
 * 获取关注总数 
 * @param {number} follow_uid 当前登录用户follow_uid
 * @return {number} 被关注用户总数
*/
const getFollowingCount = async (follow_uid) => {
    const result = await getCount(tableName, { follow_uid });
    return result
}

/**
 * 获取粉丝列表（自己关注的用户列表） 
 * @param {number} uid 当前登录用户uid
 * @return {array} 关注当前账号的用户列表
 */
const getFollowerList = async (uid) => {
    const result = await getAll(tableName, { uid });
    return result
}

/**
 * 获取粉丝总数 
 * @param {number} uid 当前登录用户uid
 * @return {number} 关注当前账号的用户总数
*/
const getFollowerCount = async (uid) => {
    const result = await getCount(tableName, { uid });
    return result
}

/**
 * 新增关注
 * @param {number} uid 当前登录用户uid
 * @param {number} follow_uid 被关注用户id  
 * @return {object} 
 */
const addFollowing = async (uid, follow_uid) => {
    // 检测关注数据是否已经存在
    if (await checkFollow(uid, follow_uid)) {
        return false;
    }
    const result = await add(tableName, { uid, follow_uid });
    return result
}

/**
 * 取消关注
 * @param {number} uid 当前登录用户uid
 * @param {number} follow_uid 被关注用户uid  
 * @return {object} 
 */
const removeFollowing = async (uid, follow_uid) => {
    const result = await remove(tableName, { uid, follow_uid });
    return result
}

module.exports = {
    getFollowingList, getFollowingCount, getFollowerList, getFollowerCount, addFollowing, removeFollowing, checkFollow
}